package cn.iocoder.yudao.module.biz.service.emergencyReservePlainDept;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.biz.controller.admin.emergencyReservePlainDept.vo.BizEmergencyReservePlainDeptCreateReqVO;
import cn.iocoder.yudao.module.biz.controller.admin.emergencyReservePlainDept.vo.BizEmergencyReservePlainDeptExportReqVO;
import cn.iocoder.yudao.module.biz.controller.admin.emergencyReservePlainDept.vo.BizEmergencyReservePlainDeptPageReqVO;
import cn.iocoder.yudao.module.biz.controller.admin.emergencyReservePlainDept.vo.BizEmergencyReservePlainDeptUpdateReqVO;
import cn.iocoder.yudao.module.biz.convert.emergencyReservePlainDept.BizEmergencyReservePlainDeptConvert;
import cn.iocoder.yudao.module.biz.dal.dataobject.emergencyReservePlainDept.BizEmergencyReservePlainDeptDO;
import cn.iocoder.yudao.module.biz.dal.mysql.emergencyReservePlainDept.BizEmergencyReservePlainDeptMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.biz.enums.ErrorCodeConstants.EMERGENCY_RESERVE_PLAIN_DEPT_NOT_EXISTS;

/**
 * 应急部门 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class BizEmergencyReservePlainDeptServiceImpl implements BizEmergencyReservePlainDeptService {

    @Resource
    private BizEmergencyReservePlainDeptMapper emergencyReservePlainDeptMapper;

    @Override
    public String createEmergencyReservePlainDept(BizEmergencyReservePlainDeptCreateReqVO createReqVO) {
        // 插入
        BizEmergencyReservePlainDeptDO emergencyReservePlainDept = BizEmergencyReservePlainDeptConvert.INSTANCE.convert(createReqVO);
        emergencyReservePlainDeptMapper.insert(emergencyReservePlainDept);
        // 返回
        return emergencyReservePlainDept.getId();
    }

    @Override
    public void updateEmergencyReservePlainDept(BizEmergencyReservePlainDeptUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateEmergencyReservePlainDeptExists(updateReqVO.getId());
        // 更新
        BizEmergencyReservePlainDeptDO updateObj = BizEmergencyReservePlainDeptConvert.INSTANCE.convert(updateReqVO);
        emergencyReservePlainDeptMapper.updateById(updateObj);
    }

    @Override
    public void deleteEmergencyReservePlainDept(String id) {
        // 校验存在
        this.validateEmergencyReservePlainDeptExists(id);
        // 删除
        emergencyReservePlainDeptMapper.deleteById(id);
    }

    private void validateEmergencyReservePlainDeptExists(String id) {
        if (emergencyReservePlainDeptMapper.selectById(id) == null) {
            throw exception(EMERGENCY_RESERVE_PLAIN_DEPT_NOT_EXISTS);
        }
    }

    @Override
    public BizEmergencyReservePlainDeptDO getEmergencyReservePlainDept(String id) {
        return emergencyReservePlainDeptMapper.selectById(id);
    }

    @Override
    public List<BizEmergencyReservePlainDeptDO> getEmergencyReservePlainDeptList(Collection<String> ids) {
        return emergencyReservePlainDeptMapper.selectBatchIds(ids);
    }

    @Override
    public List<BizEmergencyReservePlainDeptDO> getAll() {
        return emergencyReservePlainDeptMapper.selectList();
    }

    @Override
    public PageResult<BizEmergencyReservePlainDeptDO> getEmergencyReservePlainDeptPage(BizEmergencyReservePlainDeptPageReqVO pageReqVO) {
        return emergencyReservePlainDeptMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BizEmergencyReservePlainDeptDO> getEmergencyReservePlainDeptList(BizEmergencyReservePlainDeptExportReqVO exportReqVO) {
        return emergencyReservePlainDeptMapper.selectList(exportReqVO);
    }

}
